package bbs.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import bbs.entity.constants.Constants;
import bbs.entity.dto.SessionWebUserDto;
import bbs.entity.enums.*;
import bbs.entity.po.*;
import bbs.entity.query.ForumArticleAttachmentDownloadQuery;
import bbs.entity.query.UserMessageQuery;
import bbs.exception.BusinessException;
import bbs.mappers.ForumArticleAttachmentDownloadMapper;
import bbs.mappers.UserMessageMapper;
import bbs.service.ForumArticleService;
import bbs.service.UserInfoService;
import org.springframework.stereotype.Service;

import bbs.entity.query.ForumArticleAttachmentQuery;
import bbs.entity.vo.PaginationResultVO;
import bbs.entity.query.SimplePage;
import bbs.mappers.ForumArticleAttachmentMapper;
import bbs.service.ForumArticleAttachmentService;
import bbs.utils.StringTools;
import org.springframework.transaction.annotation.Transactional;


/**
 * 文件信息 业务接口实现
 */
@Service("forumArticleAttachmentService")
public class ForumArticleAttachmentServiceImpl implements ForumArticleAttachmentService {

	@Resource
	private ForumArticleAttachmentMapper<ForumArticleAttachment, ForumArticleAttachmentQuery> forumArticleAttachmentMapper;

	@Resource
	private ForumArticleAttachmentDownloadMapper<ForumArticleAttachmentDownload, ForumArticleAttachmentDownloadQuery> forumArticleAttachmentDownloadMapper;

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private ForumArticleService forumArticleService;

	@Resource
	private UserMessageMapper<UserMessage, UserMessageQuery> userMessageMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<ForumArticleAttachment> findListByParam(ForumArticleAttachmentQuery param) {
		return this.forumArticleAttachmentMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(ForumArticleAttachmentQuery param) {
		return this.forumArticleAttachmentMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<ForumArticleAttachment> findListByPage(ForumArticleAttachmentQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<ForumArticleAttachment> list = this.findListByParam(param);
		PaginationResultVO<ForumArticleAttachment> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(ForumArticleAttachment bean) {
		return this.forumArticleAttachmentMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<ForumArticleAttachment> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.forumArticleAttachmentMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<ForumArticleAttachment> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.forumArticleAttachmentMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(ForumArticleAttachment bean, ForumArticleAttachmentQuery param) {
		StringTools.checkParam(param);
		return this.forumArticleAttachmentMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(ForumArticleAttachmentQuery param) {
		StringTools.checkParam(param);
		return this.forumArticleAttachmentMapper.deleteByParam(param);
	}

	/**
	 * 根据FileId获取对象
	 */
	@Override
	public ForumArticleAttachment getForumArticleAttachmentByFileId(String fileId) {
		return this.forumArticleAttachmentMapper.selectByFileId(fileId);
	}

	/**
	 * 根据FileId修改
	 */
	@Override
	public Integer updateForumArticleAttachmentByFileId(ForumArticleAttachment bean, String fileId) {
		return this.forumArticleAttachmentMapper.updateByFileId(bean, fileId);
	}

	/**
	 * 根据FileId删除
	 */
	@Override
	public Integer deleteForumArticleAttachmentByFileId(String fileId) {
		return this.forumArticleAttachmentMapper.deleteByFileId(fileId);
	}

	/**
	 * 附件下载
	 * @param fileId
	 * @param sessionUser
	 * @return
	 */
    @Override
	@Transactional(rollbackFor = Exception.class)
    public ForumArticleAttachment downloadAttachment(String fileId, SessionWebUserDto sessionUser) {
		ForumArticleAttachment forumArticleAttachment = forumArticleAttachmentMapper.selectByFileId(fileId);
		if (forumArticleAttachment == null) throw new BusinessException(Constants.ATTACHMENT_NOT_EXISTS);

		ForumArticleAttachmentDownload download = null;
		if (forumArticleAttachment.getIntegral() > 0 && !sessionUser.getUserId().equals(forumArticleAttachment.getUserId())) {
			download = forumArticleAttachmentDownloadMapper.selectByFileIdAndUserId(fileId, sessionUser.getUserId());
			if (download == null) {
				UserInfo userInfo = userInfoService.getUserInfoByUserId(sessionUser.getUserId());
				if (userInfo.getCurrentIntegral() - forumArticleAttachment.getIntegral() < 0) throw new BusinessException(Constants.INTEGRAL_NOT_COMPLIANCE);
			}
		}

		ForumArticleAttachmentDownload updateDownload = new ForumArticleAttachmentDownload();
		updateDownload.setArticleId(forumArticleAttachment.getArticleId());
		updateDownload.setFileId(fileId);
		updateDownload.setUserId(sessionUser.getUserId());
		updateDownload.setDownloadCount(Constants.ONE);
		forumArticleAttachmentDownloadMapper.insertOrUpdate(updateDownload);
		forumArticleAttachmentMapper.updateDownloadCount(fileId);

		if (sessionUser.getUserId().equals(forumArticleAttachment.getUserId()) || download != null) return forumArticleAttachment;

//		扣除下载所需积分
		userInfoService.updateUserInternal(sessionUser.getUserId(), UserIntegralOperTypeEnum.DOWNLOAD_ATTACHMENT, UserIntegralChangeTypeEnum.REDUCE.getChangeType(), forumArticleAttachment.getIntegral());
//		给上传附件者加分
		userInfoService.updateUserInternal(forumArticleAttachment.getUserId(), UserIntegralOperTypeEnum.DOWNLOAD_ATTACHMENT, UserIntegralChangeTypeEnum.ADD.getChangeType(), forumArticleAttachment.getIntegral());
//		记录积分操作
		ForumArticle forumArticle = forumArticleService.getForumArticleByArticleId(forumArticleAttachment.getArticleId());
		UserMessage userMessage = new UserMessage();
		userMessage.setMessageType(MessageTypeEnum.DOWNLOAD_ATTACHMENT.getType());
		userMessage.setCreateTime(new Date());
		userMessage.setArticleId(forumArticleAttachment.getArticleId());
		userMessage.setArticleTitle(forumArticle.getTitle());
		userMessage.setReceivedUserId(forumArticleAttachment.getUserId());
		userMessage.setCommentId(Constants.ZERO);
		userMessage.setSendUserId(sessionUser.getUserId());
		userMessage.setSendNickName(sessionUser.getNickName());
		userMessage.setStatus(MessageStatusEnum.UNREAD.getStatus());
		userMessageMapper.insert(userMessage);
		return forumArticleAttachment;
    }
}